package cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.activitys.MainActivity;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.activitys.OtherWebActivity;
import cn.wuhan2019mwg.mobile.activity.wuhan2019mwg.utils.Constance;

/**
 * Created by dell on 2017/11/2.
 */

public abstract class BaseFragment extends Fragment {

    protected SharedPreferences mSharedPreferences;
    protected SharedPreferences.Editor mEditor;
    protected View mView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSharedPreferences = getActivity().getSharedPreferences(Constance.SHARE_KEY, getActivity().MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         if (mView == null){
             initView(inflater,container);
         }

         return mView;
    }

    protected abstract void initView(LayoutInflater inflater, ViewGroup container);
    protected  abstract  void initData();

    protected void setWebClient(final Context context, WebView webView, String url, final ProgressBar progressBar){
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        webView.getSettings().setJavaScriptEnabled(true);// 设置可用JS
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        if(width > 650)
        {
            webView.setInitialScale(190);
        }else if(width > 520)
        {
            webView.setInitialScale(160);
        }else if(width > 450)
        {
            webView.setInitialScale(140);
        }else if(width > 300)
        {
            webView.setInitialScale(120);
        }else
        {
            webView.setInitialScale(100);
        }
        
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient(){



            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                    if (request.getUrl().toString().contains("/dt")){
                        Intent intent = new Intent();
                        intent.putExtra(Constance.URL,request.getUrl().toString());
                        intent.setClass(context, OtherWebActivity.class);
                        startActivity(intent);
                    }else {
                        view.loadUrl(request.getUrl().toString());

                    }
                    Log.e("strurl","WebResourceRequest"+request.getUrl().toString());
                }





                return true;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if (url.contains("/dt") || url.contains("/Latest_news")){
                    Intent intent = new Intent();
                    intent.putExtra(Constance.URL,url);
                    intent.setClass(context, OtherWebActivity.class);
                    startActivity(intent);
                }else {
                    view.loadUrl(url);
                    Log.e("strurl", "String url " + url);
                }
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

        webView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);
                } else {
                    // 加载中
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);

                Log.e("strurl",title);
            }
        });


    }
}
